package com.ddyx.retail;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.UnsupportedEncodingException;

import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.alibaba.fastjson.JSON;
import com.ddyx.model.CylanEventRequest;
import com.ddyx.model.CylanPersonInfo;

@RunWith(SpringJUnit4ClassRunner.class)  
@WebAppConfiguration  
@ContextHierarchy({  
	@ContextConfiguration(name = "parent", locations = "classpath:META-INF/applicationContext.xml"),  
	@ContextConfiguration(name = "child", locations = "classpath:META-INF/servletContext.xml")  
})
@FixMethodOrder(MethodSorters.NAME_ASCENDING) 
public class APITestCase {
	
	@Autowired
    private WebApplicationContext webApplicationContext;
	
	private MockMvc mockMvc;
	
	public MockMvc getMockMvc() {
		return mockMvc;
	}
	
	@Before
	public void setUp() throws Exception {
		this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
	}

	@Test
	public void testCallback() throws UnsupportedEncodingException, Exception {
		/*CylanEventRequest req = new CylanEventRequest();
		req.setTime(1519545359L);
		req.setCid("290200000109");
		req.setImage_url("http://hscs.qiniudn.com/uploads/event/original_face/1578540/1516688289550_4875_0.jpg?e=1516691898&token=gBlgwMtgrEBgCTpjfXobrI9JN9JJEgqnrgqjGr-3:jkvNLCPMVWlrHQcSOHxcgjH3t1c=");
		
		CylanPersonInfo person = new CylanPersonInfo();
		person.setPerson_id("");
		person.setPerson_name("");
		person.setPerson_type(0);
		person.setSex("female");
		person.setAge("23.00");
		person.setFace_url("http://hscs.qiniudn.com/uploads/event/original_face/1578540/1516688289550_4875_0.jpg?e=1516691898&token=gBlgwMtgrEBgCTpjfXobrI9JN9JJEgqnrgqjGr-3:jkvNLCPMVWlrHQcSOHxcgjH3t1c=");
		
		req.addPerson(person);
		
		String reqJson = JSON.toJSONString(req);
		
		getMockMvc().perform(
				post("/event/callback")
				.contentType(MediaType.APPLICATION_JSON)
				.content(reqJson)
		).andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
		.andReturn().getResponse().getContentAsString();*/
	}

}
